<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />

<%@ page
	import="bibbletex.models.*,java.util.UUID,java.util.List"
	import="bibbletex.views.CollaborationView,bibbletex.controllers.CollaborationController" %>	

<%
	String page_title;
	String onloadjsfunction="";
	Collaboration c;
	String id_parameter = request.getParameter("cid");
	boolean show_library = true;
	
	if (id_parameter == null)
	{
		page_title = new String("Collaboration saved!");
		c = sessiondata.collaboration;
	}
	else
	{
		page_title = new String("Show collaboration");

		// the collaboration is editable if it was already editable and the public ID matches
		// (prevents changing from a previously editable collaboration to a new suggestion-only
		// collaboration)
		// or if the id parameter is simply the private id
		sessiondata.collaboration_is_editable =
				(sessiondata.collaboration_is_editable 
						&& sessiondata.collaboration.getPublic_id().toString().equals(id_parameter))
				||	Collaboration.id_is_private(id_parameter);

		c = CollaborationController.load(id_parameter);
		sessiondata.collaboration = c;
	}
	
	if (request.getParameter("show_references") != null
  		&& request.getParameter("show_references").equals("suggestions"))
		show_library = false;

%>

<%!	boolean show_title = true; %>
<%@ include file="/header.jsp"%>

<%	if (c != null) { %>
<div class="collabNavigation"> 
	<ul>
		<li><a href="/collaboration/show.jsp" title="Home" style="z-index:11;" class="current">Home</a></li>
		<li><a href="/collaboration/reference/import.jsp" title="Import" style="z-index:10;">Import</a></li>
		<li><a href="/collaboration/export.jsp" title="Export" style="z-index:9;">Export</a></li>
		<li><a href="/collaboration/edit.jsp"  title="Edit" style="z-index:8;" >Edit</a></li>	
		<li><a href="/collaboration/invite.jsp" title="Invite" style="z-index:7;" >Invite</a></li>
		<li><a href="delete.jsp" title="Delete" style="z-index:6;" >Delete</a></li>
		<li><a href="search.jsp" title="Search" style="z-index:5;" >Search</a></li>
	</ul>
</div>

<p>
	Title:
	<%=c.getTitle()%>
</p>

<p>
	Description:
	<%=c.getDescription()%>
</p>

<p>
	Creator:
	<%=c.getCreator_name()%>
</p>

<% if (id_parameter == null || sessiondata.collaboration_is_editable) { // We just created it so we show the links %>
<p>
	Public link:
	<a href="<%=CollaborationView.public_link(c)%>">Link</a> 
</p>

	<% if (id_parameter == null && sessiondata.collaboration_is_editable) { %> 
	<p>
		Private link:
		<a href="<%=CollaborationView.private_link(c)%>">Link</a> 
	</p>
	
	<% } %>
<% } %>

<h2>References</h2>
<div class="referenceNavigation">
  <a href="show.jsp?show_references=library" 
  		<% if (show_library) { %> class="current" <% } %>>Library</a>
  <a href="show.jsp?show_references=suggestions" 
  		<% if (!show_library) { %> class="current" <% } %>>Suggestions</a> 
</div>
<table id="referenceContainer">
   <tr class="containerHead">
       <!-- <td>ID</td> -->
        <td>Type</td>
        <td>Title</td>
        <td>Authors</td>
        <td></td>
        <td></td> 
   </tr>
<%
	List<Reference> refs;
	if (show_library)
		refs = c.getReferences();
	else
		refs = c.getSuggestions();
	
	for (Reference r : refs) {
%>
<tr class="containerBody">
	<td><%= r.getType() %></td>
	<td><a href="/collaboration/reference/show.jsp?rid=<%= r.getId() %>"><%=r.getTitle()%></a></td>
	<td><%=r.getAuthors()%></td>
	<% if (show_library) { %>
    <td class="edit"><a href="reference/edit.jsp?rid=<%= r.getId() %>" title="Edit Reference"></a></td>
    <% } else { %>
    <td class="accept"><a href="reference/accept.jsp?rid=<%= r.getId() %>" title="Accept Reference"></a></td>
    <% } %>
    <td class="delete"><a href="reference/delete.jsp?rid=<%= r.getId() %>" title="Delete Reference"></a></td>
</tr>

<%
	}
%>
</table>

<div class="buttonWrapper">
	<a class="button" href="reference/new.jsp">
		<%=CollaborationView.possible_action(sessiondata.collaboration_is_editable)%> a reference
	</a>
</div>

<% } else { %>

<p>Unable to load Collaboration</p>

<% } %>

<%@ include file="/footer.jsp"%>
